<template>
  <div>
    <form action="#">
      用户名： <input type="text" autocomplete v-model="username" />
      密码：<input type="password" autocomplete v-model="password"/>
      ID:  <input type="text" autocomplete v-model="id" />
      <button style="width: 60px; height: 25px; margin-left: 20px" @click="login">登录</button>
    </form>
  </div>
</template>

<script>
import Cookies from 'js-cookie';

export default {
  data: function() {
    return {
      username: '',
      password: '',
      id: ''
    }
  },
  methods: {
    login() {
      if(this.username == 'admin' && this.password == '1') {
        let _res = new Promise((resolve, reject) => {
          setTimeout(() => {
            let token = 'aaa123456';
            let id = this.id;

            Cookies.set('token', token);
            Cookies.set('id', id);

            if(!token) {
              reject('error');
            }

            resolve('ok');
          }, 1000);
        })

        _res.then(res => {
          if(res == 'ok') {
            alert('登录成功');
            this.$router.push('/home');
          }
        })
      }
    }
  }
}
</script>